using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class EmojiTextEx : ContentControl
	{
		private const string TAG = "EmojiTextEx";
		private Paragraph sParagraph;
		public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(EmojiTextEx), new PropertyMetadata(null, new PropertyChangedCallback(EmojiTextEx.OnTextChanged)));
		private string lastStr;
		private bool _contentLoaded;
		public string Text
		{
			get
			{
				return (string)base.GetValue(EmojiTextEx.TextProperty);
			}
			set
			{
				base.SetValue(EmojiTextEx.TextProperty, value);
			}
		}
		public EmojiTextEx()
		{
			this.InitializeComponent();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.sParagraph = (base.GetTemplateChild("sParagraph") as Paragraph);
			this.RefreshTxt();
		}
		private static void OnTextChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((EmojiTextEx)dependencyObject_0).OnTextChanged(dependencyPropertyChangedEventArgs_0);
		}
		protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshTxt();
			base.InvalidateMeasure();
		}
		private void genImage(string imgSrc)
		{
			if (string.IsNullOrEmpty(imgSrc))
			{
				return;
			}
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			Image image = new Image();
			image.set_Source(new ImageSourceConverter().ConvertFromString(imgSrc) as ImageSource);
			Binding binding = new Binding("FontSize");
			binding.set_Source(this);
			image.SetBinding(FrameworkElement.WidthProperty, binding);
			Binding binding2 = new Binding("FontSize");
			binding2.set_Source(this);
			image.SetBinding(FrameworkElement.HeightProperty, binding2);
			inlineUIContainer.set_Child(image);
			this.sParagraph.get_Inlines().Add(inlineUIContainer);
		}
		private void genTextBlock(ref string tempStr)
		{
			if (string.IsNullOrEmpty(tempStr))
			{
				return;
			}
			Run run = new Run();
			run.set_Text(tempStr);
			this.sParagraph.get_Inlines().Add(run);
			tempStr = "";
		}
		private void RefreshTxt()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (this.lastStr == this.Text)
			{
				return;
			}
			this.lastStr = this.Text;
			if (string.IsNullOrEmpty(this.Text))
			{
				this.sParagraph.get_Inlines().Clear();
				return;
			}
			this.sParagraph.get_Inlines().Clear();
			string text = "";
			for (int i = 0; i < this.Text.get_Length(); i++)
			{
				int emojiPos = EmojiManager.GetInstance().getEmojiPos(this.Text.get_Chars(i));
				if (emojiPos != -1)
				{
					this.genTextBlock(ref text);
					string emojiSrcFromFilePos = EmojiManager.GetInstance().getEmojiSrcFromFilePos(emojiPos);
					if (emojiSrcFromFilePos != null)
					{
						this.genImage(emojiSrcFromFilePos);
					}
					else
					{
						Log.smethod_1("EmojiTextEx", string.Concat(new object[]
						{
							"imgSrc == null,pos:",
							emojiPos,
							",Text[",
							i,
							"]:",
							string.Format("{0:X0000}", (int)this.Text.get_Chars(i))
						}));
					}
				}
				else
				{
					text += this.Text.get_Chars(i);
				}
			}
			this.genTextBlock(ref text);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Util/EmojiTextEx.xaml", 2));
		}
	}
}
